package leetcode;

import java.util.Arrays;

/**
 * @Project : AlgorithmLearning
 * @Package : Leetcode
 * @File : Q0322.java
 * @Author : WangRuoyu
 * @Date 2023/11/6 22:26
 */


public class Q0322 {
    /**
     * 多重背包 二维数组
     **/
//    public int coinChange(int[] coins, int amount) {
//        int[][] f = new int[coins.length + 1][amount + 1];
//        Arrays.fill(f[0], 0x3f3f3f3f);
//        f[0][0] = 0;
//        for (int i = 1; i <= coins.length; i++) {
//            for (int j = 0; j <= amount; j++) {
//                f[i][j] = f[i - 1][j];
//                if (j >= coins[i - 1]) f[i][j] = Math.min(f[i][j], f[i][j - coins[i - 1]] + 1);
//            }
//        }
//        return f[coins.length][amount] > 1e6 ? -1 : f[coins.length][amount];
//    }

    /**
     * 多重背包 一维数组优化
     **/
    public int coinChange(int[] coins, int amount) {
        int[] f = new int[amount + 1];
        Arrays.fill(f, 0x3f3f3f3f);
        f[0] = 0;
        for (int i = 0; i < coins.length; i++) {
            for (int j = 0; j <= amount; j++) {
                if (j >= coins[i]) f[j] = Math.min(f[j], f[j - coins[i]] + 1);
            }
        }
        return f[amount] > 1e6 ? -1 : f[amount];
    }

    public static void main(String[] args) {
        Q0322 sl = new Q0322();
        int[] coins = {1, 2, 5};
        int amount = 11;
        System.out.println(sl.coinChange(coins, amount));
    }
}